package com.tudouji.project.purchasebill.controller;

import com.tudouji.common.utils.poi.ExcelUtil;
import com.tudouji.framework.aspectj.lang.annotation.Log;
import com.tudouji.framework.aspectj.lang.enums.BusinessType;
import com.tudouji.framework.interceptor.annotation.RepeatSubmit;
import com.tudouji.framework.web.controller.BaseController;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.framework.web.page.TableDataInfo;
import com.tudouji.project.purchasebill.domain.Purchasebill;
import com.tudouji.project.purchasebill.service.IPurchasebillService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 采购单Controller
 *
 * @author tudouji
 * @date 2022-04-11
 */
@Slf4j
@RestController
@RequestMapping("/purchasebill/purchasebill")
public class PurchasebillController extends BaseController {
    @Autowired
    private IPurchasebillService purchasebillService;

    /**
     * 查询采购单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Purchasebill purchasebill, String status, String functionType) {
        List<Purchasebill> list = purchasebillService.selectPurchasebillList(purchasebill, status, functionType);
        return getDataTable(list);
    }

    /**
     * 查询采购单列表
     */
    @GetMapping("/listPC")
    public TableDataInfo listPC(Purchasebill purchasebill, String status, String functionType) {
        List<Purchasebill> list = purchasebillService.listPC(purchasebill, status, functionType);
        return getDataTable(list);
    }

    /**
     * 导出采购单列表
     */
    @PreAuthorize("@ss.hasPermi('purchasebill:purchasebill:export')")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Purchasebill purchasebill, String stats, String functionType) {
        List<Purchasebill> list = purchasebillService.selectPurchasebillList(purchasebill, stats, functionType);
        ExcelUtil<Purchasebill> util = new ExcelUtil<Purchasebill>(Purchasebill.class);
        return util.exportExcel(list, "purchasebill");
    }

    /**
     * 获取采购单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(purchasebillService.selectPurchasebillById(id));
    }

    /**
     * 新增采购单
     */
    @PreAuthorize("@ss.hasPermi('purchasebill:purchasebill:add')")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Purchasebill purchasebill) {
        return toAjax(purchasebillService.insertPurchasebill(purchasebill));
    }

    /**
     * 修改采购单
     */
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Purchasebill purchasebill) {
        return toAjax(purchasebillService.updatePurchasebill(purchasebill));
    }

    /**
     * 删除采购单
     */
    @PreAuthorize("@ss.hasPermi('purchasebill:purchasebill:remove')")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{guids}")
    public AjaxResult remove(@PathVariable String[] guids) {
        return toAjax(purchasebillService.deletePurchasebillByIds(guids));
    }


    /**
     * 发起
     */
    @RepeatSubmit
    @Log(title = "发起", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody Purchasebill purchasebill, String fileIds) {
        return purchasebillService.save(purchasebill, fileIds);
    }

    /**
     * PC添加采购单
     */
    @RepeatSubmit
    @Log(title = "PC添加采购单", businessType = BusinessType.INSERT)
    @PostMapping("/pc/save")
    public AjaxResult savePc(@RequestBody Purchasebill purchasebill) {
        log.info("--------/pc/save");
        log.info(purchasebill.toString());
        log.info("/pc/save--------");
        return purchasebillService.savePc(purchasebill);
    }

    /**
     * PC发起
     */
    @RepeatSubmit
    @Log(title = "PC发起", businessType = BusinessType.OTHER)
    @PostMapping("/pc/start")
    public AjaxResult start(@RequestBody Purchasebill purchasebill) {
        return purchasebillService.startPc(purchasebill.getId(), null);
    }

    @RepeatSubmit
    @Log(title = "审批", businessType = BusinessType.OTHER)
    @PostMapping("/approval")
    public AjaxResult approval(Long taskid, Long id, String opinion, @RequestParam(value = "fileIds", required = false) String imgids) {
        return purchasebillService.approval(id, imgids, opinion, taskid);
    }

    @RepeatSubmit
    @Log(title = "入库", businessType = BusinessType.OTHER)
    @PostMapping("/transferIn")
    public AjaxResult transferIn(Long taskid, Long id, String opinion, @RequestBody Purchasebill base
            , @RequestParam(value = "fileIds", required = false) String imgids, @RequestParam(required = false) BigDecimal exchangerate) {
        logger.info(base.getWhinitems().toString());
        return purchasebillService.transferIn(id, base.getWhinitems(), imgids, opinion, taskid, exchangerate);
    }

    @RepeatSubmit
    @Log(title = "反入库", businessType = BusinessType.OTHER)
    @PostMapping("/backOut")
    public AjaxResult backOut(Long taskid, Long id, String opinion, @RequestParam(value = "fileIds", required = false) String imgids) {
        return purchasebillService.whinBack(id, imgids, opinion, taskid);
    }

    @RepeatSubmit
    @Log(title = "拒绝", businessType = BusinessType.OTHER)
    @PostMapping("/refuse")
    public AjaxResult refuse(Long taskid, Long id, String opinion, String fileIds, Long refuseNodeid, Long backUserid) {
        return purchasebillService.refuse(id, fileIds, opinion, taskid, refuseNodeid, backUserid);
    }

    @RepeatSubmit
    @Log(title = "删除", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult del(Long id) {
        return purchasebillService.del(id);
    }

    @RepeatSubmit
    @Log(title = "PC删除", businessType = BusinessType.DELETE)
    @PostMapping("/pc/del")
    public AjaxResult delPc(Long id) {
        return purchasebillService.delPc(id);
    }
}
